package com.zxn.xiaoran.presenter;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.zxn.presenter.presenter.BasePresenter;
import com.zxn.presenter.presenter.IView;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by zxn on 2019/5/8.
 */
public class XMPresenter<V extends IView> extends BasePresenter<V> {

    public abstract class XMObserver<T> implements Observer<T> {
        private RefreshLayout mRefreshLayout;

        public XMObserver() {

        }

        public XMObserver(RefreshLayout refreshLayout) {
            this.mRefreshLayout = refreshLayout;
        }

        @Override
        public void onSubscribe(Disposable d) {
            showLoading();
        }

        @Override
        public void onError(Throwable e) {
            closeLoading();
            if (null != mRefreshLayout) {
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadMore();
            }
        }

        @Override
        public void onComplete() {
            closeLoading();
            if (null != mRefreshLayout) {
                if (isRefresh()) {
                    mRefreshLayout.resetNoMoreData();
                }
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadMore();
            }
        }
    }

    public boolean isRefresh() {
        return mCurrentPage == 1;
    }
}
